home *** CD-ROM | disk | FTP | other *** search
/ Robotics & Artificial Int…3 (Professional Edition) / Robotics & Artificial Intelligence Tools 2003 (Professional Edition).iso / neural network tool and application / nsinstall.exe / data1.cab / CodeGen_Files / EXAMPLES / XOR3.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-08  |  8.2 KB  |  222 lines

  1. // Xor3Test.cpp
  2. // Automatically generated from breadboard by NeuroSolutions.
  3.  
  4. #include "NSLib.h" 
  5.  
  6.  
  7. void saveAllWeights(char *weightsFilePath);
  8. BOOL networkStopped=FALSE;
  9. unsigned short int weightFileVersion=100;
  10. BOOL forceBackpropLearning=FALSE, backpropLearningOn=TRUE;
  11.  
  12. // Component Construction
  13.     Axon axon1;
  14.     BackAxon backAxon25;
  15.     File file35;
  16.     FullSynapse fullSynapse2;
  17.     BackFullSynapse backFullSynapse26;
  18.     Momentum momentum27;
  19.     TanhAxon tanhAxon6;
  20.     BackTanhAxon backTanhAxon28;
  21.     Momentum momentum29;
  22.     FullSynapse fullSynapse10;
  23.     BackFullSynapse backFullSynapse30;
  24.     Momentum momentum31;
  25.     TanhAxon tanhAxon14;
  26.     BackTanhAxon backTanhAxon32;
  27.     Momentum momentum33;
  28.     L2Criterion l2Criterion18;
  29.     BackCriteriaControl backCriteriaControl34;
  30.     File file36;
  31.     File dataWriter;
  32.  
  33. int main() {
  34.     srand((unsigned)time(NULL));
  35.  
  36.     // Component Initialization
  37.     axon1.setRows(2);
  38.     backAxon25.setRows(2);
  39.     // This section of code was removed after NeuroSolutions generated the code for the BB
  40.     // (Begin)
  41.     //file35.setFilePath("file35.bin");
  42.     //file35.setMode(READ,BINARY);
  43.     // (End)
  44.  
  45.     // This section of code was written after NeuroSolutions generated the code for the BB
  46.     // (Begin)
  47.     char    m_sInFilePath[512];
  48.     printf("Enter the name of the input file:\n");
  49.     scanf("%s",m_sInFilePath);
  50.     file35.setFilePath(m_sInFilePath);
  51.     file35.setMode(READ,ASCII);
  52.     // (End)
  53.     
  54.     file35.setSpatialDimension(2,1);
  55.     momentum27.setDefaultMomentum((NSFloat)0.700000);
  56.     momentum27.setDefaultStepSize((NSFloat)0.250000);
  57.     tanhAxon6.setRows(4);
  58.     backTanhAxon28.setOffset((NSFloat)0.100000);
  59.     backTanhAxon28.setRows(4);
  60.     momentum29.setDefaultMomentum((NSFloat)0.700000);
  61.     momentum29.setDefaultStepSize((NSFloat)0.250000);
  62.     momentum31.setDefaultMomentum((NSFloat)0.700000);
  63.     momentum31.setDefaultStepSize((NSFloat)0.025000);
  64.     tanhAxon14.setRows(1);
  65.     backTanhAxon32.setOffset((NSFloat)0.100000);
  66.     backTanhAxon32.setRows(1);
  67.     momentum33.setDefaultMomentum((NSFloat)0.700000);
  68.     momentum33.setDefaultStepSize((NSFloat)0.025000);
  69.     l2Criterion18.setRows(1);
  70.     backCriteriaControl34.setRows(1);
  71.     file36.setFilePath("file36.bin");
  72.     file36.setMode(READ,BINARY);
  73.     file36.setSpatialDimension(1,1);
  74.     dataWriter.setMode(WRITE,STANDARD);
  75.     dataWriter.setSpatialDimension(1,1);
  76.     FILE *loadStream = fopen("Xor.nsw","r");
  77.     if (!loadStream) {
  78.         fprintf(stderr, "Could not open weight file Xor.nsw");
  79.         exit(1);
  80.     }
  81.     weightFileVersion = getWeightFileVersion(loadStream);
  82.  
  83.     // Load Normalization Coefficients of Files
  84.     file35.loadWeights(seekComponent(loadStream, "File", "file35"),weightFileVersion);
  85.      file36.loadWeights(seekComponent(loadStream, "File", "file36"),weightFileVersion);
  86.  
  87.     // Component Interconnection
  88.     axon1.setPreActivityAccess(&file35);
  89.     backAxon25.setDual(&axon1);
  90.     backFullSynapse26.setDual(&fullSynapse2);
  91.     backTanhAxon28.setDual(&tanhAxon6);
  92.     backFullSynapse30.setDual(&fullSynapse10);
  93.     backTanhAxon32.setDual(&tanhAxon14);
  94.     l2Criterion18.setCostAccess(&dataWriter);
  95.     l2Criterion18.setDesiredAccess(&file36);
  96.     l2Criterion18.setSensitivityTotalsData(NULL);
  97.     l2Criterion18.setSensitivityData(NULL);
  98.     backCriteriaControl34.setDual(&l2Criterion18);
  99.     axon1.setNext(&fullSynapse2);
  100.     backAxon25.setLast(&backFullSynapse26);
  101.     fullSynapse2.setLast(&axon1);
  102.     fullSynapse2.setNext(&tanhAxon6);
  103.     backFullSynapse26.setLast(&backTanhAxon28);
  104.     backFullSynapse26.setNext(&backAxon25);
  105.     tanhAxon6.setLast(&fullSynapse2);
  106.     tanhAxon6.setNext(&fullSynapse10);
  107.     backTanhAxon28.setLast(&backFullSynapse30);
  108.     backTanhAxon28.setNext(&backFullSynapse26);
  109.     fullSynapse10.setLast(&tanhAxon6);
  110.     fullSynapse10.setNext(&tanhAxon14);
  111.     backFullSynapse30.setLast(&backTanhAxon32);
  112.     backFullSynapse30.setNext(&backTanhAxon28);
  113.     tanhAxon14.setLast(&fullSynapse10);
  114.     tanhAxon14.setNext(&l2Criterion18);
  115.     backTanhAxon32.setLast(&backCriteriaControl34);
  116.     backTanhAxon32.setNext(&backFullSynapse30);
  117.     l2Criterion18.setLast(&tanhAxon14);
  118.     backCriteriaControl34.setNext(&backTanhAxon32);
  119.     momentum27.setErrorSoma(&backFullSynapse26);
  120.     momentum29.setErrorSoma(&backTanhAxon28);
  121.     momentum31.setErrorSoma(&backFullSynapse30);
  122.     momentum33.setErrorSoma(&backTanhAxon32);
  123.  
  124.     // Load Axon Weights
  125.     // This section of code was written after NeuroSolutions generated the code for the BB
  126.     // (Begin)
  127.     printf("Do you want to load (l) the weights or use random (r) weights?\n");
  128.     char response[128];
  129.     scanf("%s",response);
  130.     if ((response[0] == 'l') || (response[0] == 'L')) {
  131.     // (End)
  132.         axon1.loadWeights(seekComponent(loadStream, "Axon", "axon1"),weightFileVersion);
  133.         tanhAxon6.loadWeights(seekComponent(loadStream, "TanhAxon", "tanhAxon6"),weightFileVersion);
  134.         tanhAxon14.loadWeights(seekComponent(loadStream, "TanhAxon", "tanhAxon14"),weightFileVersion);
  135.         l2Criterion18.loadWeights(seekComponent(loadStream, "L2Criterion", "l2Criterion18"),weightFileVersion);
  136.  
  137.  
  138.         // Load Synapse Weights
  139.         fullSynapse2.loadWeights(seekComponent(loadStream, "FullSynapse", "fullSynapse2"),weightFileVersion);
  140.         fullSynapse10.loadWeights(seekComponent(loadStream, "FullSynapse", "fullSynapse10"),weightFileVersion);
  141.         momentum27.loadWeights(seekComponent(loadStream, "Momentum", "momentum27"),weightFileVersion);
  142.         momentum29.loadWeights(seekComponent(loadStream, "Momentum", "momentum29"),weightFileVersion);
  143.         momentum31.loadWeights(seekComponent(loadStream, "Momentum", "momentum31"),weightFileVersion);
  144.         momentum33.loadWeights(seekComponent(loadStream, "Momentum", "momentum33"),weightFileVersion);
  145.     // (Begin)
  146.     }
  147.     // (End)
  148.      fclose(loadStream);
  149.  
  150.     // Get Ready to Run Network
  151.     int updateCounter=0;
  152.  
  153.     // Run Network
  154.     // This section of code was written after NeuroSolutions generated the code for the BB
  155.     // (Begin)
  156.     unsigned int    numberOfEpochs = 0;
  157.     printf("Enter the number of epochs to train for:\n");
  158.     scanf("%d",&numberOfEpochs);
  159.     for (unsigned int epoch=0; epoch<numberOfEpochs; epoch++) { 
  160.     // (End)
  161.     // for (unsigned int epoch=0; epoch<100; epoch++) {   (Generated Code)
  162.         for (unsigned int exemplar=0; exemplar<4; exemplar++) { 
  163.             axon1.fire();
  164.             if (backpropLearningOn || forceBackpropLearning) {
  165.                 backAxon25.backpropStarting();
  166.                 backFullSynapse26.backpropStarting();
  167.                 backTanhAxon28.backpropStarting();
  168.                 backFullSynapse30.backpropStarting();
  169.                 backTanhAxon32.backpropStarting();
  170.                 backCriteriaControl34.backpropStarting();
  171.                 backCriteriaControl34.fire();
  172.                 backAxon25.backpropComplete();
  173.                 backFullSynapse26.backpropComplete();
  174.                 backTanhAxon28.backpropComplete();
  175.                 backFullSynapse30.backpropComplete();
  176.                 backTanhAxon32.backpropComplete();
  177.                 backCriteriaControl34.backpropComplete();
  178.                 if (++updateCounter >= 4) {
  179.                     NSFloat cost3 = l2Criterion18.reportCost();
  180.                     momentum27.updateWeights(&fullSynapse2);
  181.                     momentum29.updateWeights(&tanhAxon6);
  182.                     momentum31.updateWeights(&fullSynapse10);
  183.                     momentum33.updateWeights(&tanhAxon14);
  184.                     updateCounter = 0;
  185.                 }
  186.                 forceBackpropLearning = FALSE;
  187.             }
  188.             if (networkStopped)
  189.                 goto ConcludeFiring;
  190.         }
  191.     }
  192.     
  193.     ConcludeFiring:
  194.     saveAllWeights("Xor.nsw");
  195.     l2Criterion18.reportCost();
  196.  
  197.     return 0;
  198. }
  199.  
  200. void saveAllWeights(char *weightsFilePath)
  201. {    FILE *saveStream = fopen(weightsFilePath,"w");
  202.     if (!saveStream) {
  203.         fprintf(stderr, "Could not open weight file %s\n", weightsFilePath);
  204.         exit(1);
  205.     }
  206.     fprintf(saveStream,"#NSWeightFileVersion 137\n\n");
  207.     file35.saveWeights(putComponent(saveStream, "File", "file35"));
  208.      file36.saveWeights(putComponent(saveStream, "File", "file36"));
  209.      axon1.saveWeights(putComponent(saveStream, "Axon", "axon1"));
  210.      fullSynapse2.saveWeights(putComponent(saveStream, "FullSynapse", "fullSynapse2"));
  211.      tanhAxon6.saveWeights(putComponent(saveStream, "TanhAxon", "tanhAxon6"));
  212.      fullSynapse10.saveWeights(putComponent(saveStream, "FullSynapse", "fullSynapse10"));
  213.      tanhAxon14.saveWeights(putComponent(saveStream, "TanhAxon", "tanhAxon14"));
  214.      l2Criterion18.saveWeights(putComponent(saveStream, "L2Criterion", "l2Criterion18"));
  215.      momentum27.saveWeights(putComponent(saveStream, "Momentum", "momentum27"));
  216.      momentum29.saveWeights(putComponent(saveStream, "Momentum", "momentum29"));
  217.      momentum31.saveWeights(putComponent(saveStream, "Momentum", "momentum31"));
  218.      momentum33.saveWeights(putComponent(saveStream, "Momentum", "momentum33"));
  219.      fclose(saveStream);
  220. }
  221.  
  222.